﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class LoaiNhanVienDAO
    {
        SqlConnection conn = null;

        public List<LoaiNhanVienDTO> LayDanhSachLoaiNhanVien()
        {
            List<LoaiNhanVienDTO> lisLNV = new List<LoaiNhanVienDTO>();
            SqlDataReader reader = null;
            try
            {
                conn = DataProvider.ConnectDB();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_LayDanhSachLoaiNhanVien";
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    LoaiNhanVienDTO lnvDTO = new LoaiNhanVienDTO();
                    lnvDTO.MaLoaiNhanVien = reader.GetInt32(0);
                    lnvDTO.TenLoaiNhanVien = reader.GetString(1);

                    lisLNV.Add(lnvDTO);
                }
            }
            catch (Exception ex)
            { String e = ex.ToString(); }
            finally
            { conn.Close(); }
            return lisLNV;
        }

        public LoaiNhanVienDTO LayThongTinLoaiNhanVienTheoMa(int ma)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cm = new SqlCommand();
            cm.Connection = conn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "usp_LayThongTinLoaiNhanVienTheoMa";
            cm.Parameters.Add("@MaLoaiNhanVien", SqlDbType.Int).Value = ma;
            SqlDataReader dr = cm.ExecuteReader();
            LoaiNhanVienDTO dto = new LoaiNhanVienDTO();
            dr.Read();
            dto.MaLoaiNhanVien = dr.GetInt32(0);
            dto.TenLoaiNhanVien = dr.GetString(1);
            dr.Close();
            conn.Close();
            return dto;
        }

        public List<LoaiNhanVienDTO> LayThongTinLoaiNhanVienTheoMa2(int ma)
        {
            List<LoaiNhanVienDTO> lisLNV = new List<LoaiNhanVienDTO>();
            SqlDataReader reader = null;
            try
            {
                conn = DataProvider.ConnectDB();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_LayThongTinLoaiNhanVienTheoMa";
                cmd.Parameters.Add("@MaLoaiNhanVien", SqlDbType.Int).Value = ma;
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    LoaiNhanVienDTO lnvDTO = new LoaiNhanVienDTO();
                    lnvDTO.MaLoaiNhanVien = reader.GetInt32(0);
                    lnvDTO.TenLoaiNhanVien = reader.GetString(1);

                    lisLNV.Add(lnvDTO);
                }
            }
            catch (Exception ex)
            { String e = ex.ToString(); }
            finally
            { conn.Close(); }
            return lisLNV;
        }

        public string ThemLoaiNhanVien(LoaiNhanVienDTO lnvDTO)
        {
            string msg = "";
            try
            {
                conn = DataProvider.ConnectDB();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_ThemLoaiNhanVien";
                SqlParameter para;
                para = cmd.Parameters.Add("@TenLoaiNhanVien", SqlDbType.NVarChar);
                para.Value = lnvDTO.TenLoaiNhanVien;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (SqlException ex)
            {
                msg = ex.Message;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return msg;
        }

        public string CapNhatLoaiNhanVien(LoaiNhanVienDTO lnvDTO)
        {
            string msg = "";
            try
            {
                conn = DataProvider.ConnectDB();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_CapNhatLoaiNhanVien";
                SqlParameter para;
                para = cmd.Parameters.Add("@MaLoaiNhanVien", SqlDbType.Int);
                para.Value = lnvDTO.MaLoaiNhanVien;
                para = cmd.Parameters.Add("@TenLoaiNhanVien", SqlDbType.NVarChar);
                para.Value = lnvDTO.TenLoaiNhanVien;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (SqlException ex)
            {
                msg = ex.Message;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return msg;
        }

        public string XoaLoaiNhanVien(LoaiNhanVienDTO lnvDTO)
        {
            string msg = "";
            try
            {
                conn = DataProvider.ConnectDB();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_XoaLoaiNhanVien";
                SqlParameter para;
                para = cmd.Parameters.Add("@MaLoaiNhanVien", SqlDbType.Int);
                para.Value = lnvDTO.MaLoaiNhanVien;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (SqlException ex)
            {
                msg = ex.Message;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return msg;
        }
    }
}
